﻿using System.Windows;
using System.Windows.Controls;
using Parse;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for ParseEditTask.xaml
    /// </summary>
    public partial class ParseEditTask : UserControl
    {
        ParseObjectMask _obj = null;

        // ctr 
        public ParseEditTask(ParseObjectMask theTask)
        {
            _obj = theTask;
            InitializeComponent();
        }
        public ParseEditTask()
        {
            InitializeComponent();
        }

        // overrides 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_obj != null)
            {
                this.txtName.Text = _obj.Po.Get<string>("Title");
            }
        }

        // Events 
        private void FwButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.UpdateTask();
        }
        private void btnTaskDelete_Click(object sender, RoutedEventArgs e)
        {
            this.DeleteTask();
        }

        // Methods 
        private async void UpdateTask()
        {
            if (_obj != null)
            {
                // Context 
                ParseDataContext ctx = ((HostWindow)Application.Current.MainWindow)
                        .DataContext as ParseDataContext;

                if (ctx == null)
                    return;

                if (ctx.SelectedApp == null)
                    return;

                ParseApp app = ctx.SelectedApp as ParseApp;

                if (app != null)
                {
                    _obj.Po["Title"] = this.txtName.Text;
                    await ParseObjectMask.UpdateItemAsync(_obj, app);

                    ParseOne.InformationSending(new ParseCustomEventArgs("Task:Refresh"));
                }
            }

            // Close Panel 
            ParseOne.InformationSending(new ParseCustomEventArgs("Panel:Close"));
        }
        private async void DeleteTask()
        {
            if (_obj != null)
            {
                // Context 
                ParseDataContext ctx = ((HostWindow)Application.Current.MainWindow)
                        .DataContext as ParseDataContext;

                if (ctx == null)
                    return;

                if (ctx.SelectedApp == null)
                    return;

                ParseApp app = ctx.SelectedApp as ParseApp;

                if (app != null)
                {
                    await ParseObjectMask.DeleteItemAsync(_obj, app);

                    ParseOne.InformationSending(new ParseCustomEventArgs("Task:Refresh"));
                }
            }

            // Close Panel 
            ParseOne.InformationSending(new ParseCustomEventArgs("Panel:Close"));
        }
    }
}
